package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.BrochureMedia;
	import com.ettractions.kiosk.events.BrochureSelectedEvent;
	import com.ettractions.kiosk.events.PreRenderBrochureEvent;
	import com.ettractions.kiosk.vo.BrochureMediaVO;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import mx.collections.ArrayList;
	
	import org.osmf.events.TimeEvent;
	import org.robotlegs.mvcs.Mediator;
		
	public class BrochureMediaMediator extends Mediator
	{
		private static const IMAGE_DISPLAY_DURATION:uint = 2000;
		
		// "Dots" List component is reporting incorrect width and not centering correctly; these hard-coded values allow for proper centering of that list
		private static const DOTS_LIST_ITEM_WIDTH:uint = 10;
		private static const DOTS_LIST_ITEM_GAP:uint = 4;
		
		[Inject]
		public var view:BrochureMedia;
		
		
		private var _imageLoader:Loader = new Loader();
		
		private var _imageRotationTimer:Timer = new Timer(IMAGE_DISPLAY_DURATION, 1);
		
		private var _loadingImagePath:String;
		
		private var _images:Object;		
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(PreRenderBrochureEvent.PRE_RENDER_BROCHURE, handlePreRenderBrochure);
			eventDispatcher.addEventListener(BrochureSelectedEvent.BROCHURE_SELECTED, handleBrochureSelected);
			
			view.video.visible = false;
			view.showPhotosButton.visible = false;
			
			view.playVideoButton.addEventListener(MouseEvent.CLICK, handlePlayVideoButtonClick);
			view.showPhotosButton.addEventListener(MouseEvent.CLICK, handleShowPhotosButtonClick);
			
			_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoaderComplete);
			
			_imageRotationTimer.addEventListener(TimerEvent.TIMER_COMPLETE, handleRotatorTimerComplete);
			
		}

		private function updateView(brochure:BrochureVO):void
		{			
			var isSpecial:Boolean = brochure.specialTag == "TRUE" ? true : false;
			
			view.main_specials.visible = isSpecial;
			
		}
		
		private function handlePreRenderBrochure(event:PreRenderBrochureEvent):void
		{
			view.image.source = event.brochure.media.firstContentImage;
			updateView(event.brochure);
		}
		
		private function handleBrochureSelected(event:BrochureSelectedEvent):void
		{

			_imageRotationTimer.reset();
			
			if (event.brochure != null)
			{
				var media:BrochureMediaVO = event.brochure.media;
				var dotsDataProvider:ArrayList = new ArrayList();
				
				_images = {}
				
				updateView(event.brochure);

				for each (var imagePath:String in media.contentImagePaths)
				{
					dotsDataProvider.addItem(imagePath);
					
					_images[imagePath] = null;
				}
				
				_images[media.contentImagePaths[0]] = media.firstContentImage;
				
				view.data = event.brochure;
					
				// Component is reporting incorrect width; using constant values to properly center it
				view.dots.x = Math.round((view.image.width / 2) - (((DOTS_LIST_ITEM_WIDTH * dotsDataProvider.length) + (DOTS_LIST_ITEM_GAP * (dotsDataProvider.length - 1))) / 2));
	
				view.dots.dataProvider = dotsDataProvider;
				view.dots.selectedIndex = 0;
				
				view.imageRotator.visible = true;
				
				view.showPhotosButton.visible = false;
				view.video.visible = false;

				_imageRotationTimer.start();
			}
		}
		
		private function handlePlayVideoButtonClick(event:MouseEvent):void
		{
			_imageRotationTimer.reset();
			
			view.imageRotator.visible = false;
			
			view.video.visible = true;
			view.showPhotosButton.visible = true;
			view.video.play();
		}
		
		private function handleShowPhotosButtonClick(event:MouseEvent):void
		{
			_imageRotationTimer.start();
			
			view.video.stop();
			view.video.visible = false;
			view.showPhotosButton.visible = false;

			view.imageRotator.visible = true;
		}

		private function handleVideoComplete(event:TimeEvent):void
		{
			view.video.visible = false;
			view.showPhotosButton.visible = false;
			
			view.imageRotator.visible = true;
			
			view.dots.selectedIndex = 0;
			
			_imageRotationTimer.start();
		}
		
		private function handleImageLoaderComplete(event:Event):void
		{
			var bitmapData:BitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
			var index:uint = view.dots.selectedIndex + 1;
			
			view.image.source = bitmapData;
			
			view.dots.selectedIndex = index;
			
			_images[_loadingImagePath] = bitmapData;
			
			_imageRotationTimer.start();			
		}
		
		private function handleRotatorTimerComplete(event:TimerEvent):void
		{
			var dp:ArrayList = ArrayList(view.dots.dataProvider);
			var index:uint = view.dots.selectedIndex + 1;
			var bitmapData:BitmapData;
			
			if (index == dp.length)
				index = 0;
			
			_imageRotationTimer.reset();
			
			_loadingImagePath = String(dp.getItemAt(index));
			
			bitmapData = _images[_loadingImagePath];
			
			if (bitmapData != null)
			{
				view.image.source = bitmapData;
				
				view.dots.selectedIndex = index;				
				
				_imageRotationTimer.start();
			}
				
			else
				_imageLoader.load(new URLRequest(_loadingImagePath));
		}
	}
}